/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-26 18:32:44
 * @Description  : 激活码接口验证中间件
 */

const Joi = require('joi')
const { Op } = require('sequelize')
const R = require('@/models/Response')
const { ActiveCode } = require('@/models/model')
const { ERROR_SYSTEM_CODE, ERROR_USER_CODE } = require('@/constants/errorCode')
const { IS_INTEGER, IS_UNSAFE_INTEGER } = require('@/constants/joiSchema')
const { dumpCheck: globalDumpCheck } = require('@/utils/dumpCheck')

const activeCodeSchema = {
  activeCodeIds: Joi.array().items(IS_UNSAFE_INTEGER),
  activeCodeId: IS_UNSAFE_INTEGER,
  page: IS_INTEGER.min(1).default(1),
  pageSize: IS_INTEGER.min(1).default(999),
  // isActive: Joi.invalid(0, 1),
  examId: IS_UNSAFE_INTEGER
}

const dumpCheck = {
  /**
   * @author: Hejh(3010733382@qq.com)
   * @description: 验证激活码ID的合法性
   * @param {array<string>} activeCodeIds 激活码ID数组
   */
  checkActiveCodeIds: async (activeCodeIds) => {
    let newactiveCodeIds = [...new Set(activeCodeIds)]
    // 不能出现重复的
    if (newactiveCodeIds.length !== activeCodeIds.length) {
      return false
    }

    newactiveCodeIds = newactiveCodeIds.map((item) => BigInt(item))

    if (newactiveCodeIds.length === 0) {
      return true
    }

    // 查看不重复的和数据库的是否对的上
    const myWhere = {
      id: {
        [Op.in]: newactiveCodeIds
      }
    }
    const activeCodes = await ActiveCode.findAll({
      where: myWhere
    })
    if (activeCodes.length !== newactiveCodeIds.length) {
      return false
    }

    return true
  }
}

/**
 * @description 激活码列表查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listActiveCodeValidate = async (ctx, next) => {
  const schema = Joi.object({
    page: activeCodeSchema.page,
    pageSize: activeCodeSchema.pageSize,
    examId: activeCodeSchema.examId
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  ctx.newQuery = schema.value
  await next()
}

/**
 * @description 创建激活码时的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.createActiveCodeValidate = async (ctx, next) => {
  const schema = Joi.object({
    examId: activeCodeSchema.examId.required()
  }).validate(ctx.request.body)

  const { examId } = ctx.request.body

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  // TODO 验证examId的合法性
  const isValid = await globalDumpCheck.checkExamIds([examId])
  if (!isValid) {
    ctx.body = R.error(ERROR_USER_CODE.EXAM_ID_ERROR)
    return
  }
  await next()
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 批量删除激活码接口的参数验证
 * @param {*} ctx
 * @param {*} next
 */
module.exports.delActiveCodesValidate = async (ctx, next) => {
  const schema = Joi.object({
    activeCodeIds: activeCodeSchema.activeCodeIds.required()
  }).validate(ctx.request.body)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }

  const { activeCodeIds } = ctx.request.body

  // 验证激活码ID数组的合法性
  const b4 = await dumpCheck.checkActiveCodeIds(activeCodeIds)
  if (!b4) {
    ctx.body = R.error(ERROR_USER_CODE.ACTIVE_CODE_ID_ERROR)
    return
  }
  await next()
}
